package com.byt.framlib.commonwidget;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.annotation.ColorInt;
import android.text.Editable;
import android.text.Html;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.byt.framlib.R;
import com.byt.framlib.commonutils.SolveEditTextScrollClash;


/**
 * 编辑框清除按钮
 * android:drawableRight="@drawable/custom_icon
 */
@SuppressLint("AppCompatCustomView")
public class ClearaEditRelative extends RelativeLayout {

    private EditText ed_edit_relative_value;
    private TextView tv_edit_relative_hint;
    private Context context;

    public ClearaEditRelative(Context context) {
        super(context, null);
        this.context = context;
    }

    public ClearaEditRelative(final Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        View.inflate(context, R.layout.layout_cleara_edit_relative, this);
        ed_edit_relative_value = findViewById(R.id.ed_edit_relative_value);
        tv_edit_relative_hint = findViewById(R.id.tv_edit_relative_hint);
        ed_edit_relative_value.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tv_edit_relative_hint.setText(Html.fromHtml("<font color =#272727>还可以输入</font><font color =#ff487d>" + (200 - s.length()) + "</font>" +
                        "<font color =#272727>字</font>"));
            }
        });
        ed_edit_relative_value.setOnTouchListener(new SolveEditTextScrollClash(ed_edit_relative_value));
    }

    public void setInitData(String data) {
        if (!TextUtils.isEmpty(data)) {
            ed_edit_relative_value.setText(data);
            tv_edit_relative_hint.setText(Html.fromHtml("<font color =#272727>还可以输入</font><font color =#ff487d>" + (200 - data.length()) + "</font>" +
                    "<font color =#272727>字</font>"));
        } else {
            ed_edit_relative_value.setText("");
            tv_edit_relative_hint.setText(Html.fromHtml("<font color =#272727>还可以输入</font><font color =#ff487d>" + 200 + "</font>" +
                    "<font color =#272727>字</font>"));
        }
    }

    public void setTextHint(String hint) {
        ed_edit_relative_value.setHint(hint);
    }

    public void setMaxLength(int length) {
        ed_edit_relative_value.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
    }

    public void setTextSize(float size) {
        ed_edit_relative_value.setTextSize(size);
    }

    public void setTextHintColor(@ColorInt int color) {
        ed_edit_relative_value.setHintTextColor(color);
    }

    public void setTextColor(@ColorInt int color) {
        ed_edit_relative_value.setTextColor(color);
    }

    public String getTextValue() {
        return ed_edit_relative_value.getText().toString();
    }
}